/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdaboostM1SettingsPanel.java
 *
 * Created on Dec 11, 2009, 1:33:23 PM
 */

package wekinator.LearningAlgorithms;

import javax.swing.JPanel;

/**
 *
 * @author rebecca
 */
public class IbkSettingsPanel extends javax.swing.JPanel implements LearningAlgorithmSettingsPanel {

    IbkLearningAlgorithm la = null;

    /** Creates new form AdaboostM1SettingsPanel */
    public IbkSettingsPanel() {
        initComponents();
    }

    public IbkSettingsPanel(IbkLearningAlgorithm la) {
        initComponents();
        setLearningAlgorithm(la);
    }

    public void setLearningAlgorithm(IbkLearningAlgorithm la) {
        this.la = la;
        textNumNeighbors.setText(la.getClassifier().getKNN() + "");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        textNumNeighbors = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        textNumNeighbors.setText("100");

        jLabel1.setText("Number of neighbors (k)");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(textNumNeighbors, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 54, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(34, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(textNumNeighbors, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(jLabel1))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void applySettings() throws Exception {
        boolean valid = validateSettings();
        if (valid) {
            int i = Integer.parseInt(textNumNeighbors.getText());
            la.getClassifier().setKNN(i);
        } else {
            throw new Exception("Invalid settings");
        }
    }

    public boolean validateSettings() {
        try {
            int i = Integer.parseInt(textNumNeighbors.getText());
            if (i > 0)
                return true;
        } catch (Exception ex) {
            return false;
        }
        return false;
    }

    public void reset() {
        textNumNeighbors.setText(la.getClassifier().getKNN()+ "");
    }

    public JPanel getPanel() {
        return this;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField textNumNeighbors;
    // End of variables declaration//GEN-END:variables

}
